package org.example.backend.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.example.backend.dto.LoginDTO;
import org.example.backend.dto.LoginResponseDTO;
import org.example.backend.dto.RegisterDTO;
import org.example.backend.entity.User;
import org.example.backend.mapper.UserMapper;
import org.example.backend.service.EmailService;
import org.example.backend.service.UserService;
import org.example.backend.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private EmailService emailService;

    @Override
    public LoginResponseDTO login(LoginDTO loginDTO) {
        // 根据用户名查询用户
        User user = getByUsername(loginDTO.getUsername());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 验证密码
        if (!BCrypt.checkpw(loginDTO.getPassword(), user.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        // 验证状态
        if (user.getStatus() == 0) {
            throw new RuntimeException("账号已被禁用");
        }

        // 生成token
        String token = jwtUtil.generateToken(user.getId(), user.getUsername(), "USER");

        // 构造返回结果
        LoginResponseDTO responseDTO = new LoginResponseDTO();
        responseDTO.setUserId(user.getId());
        responseDTO.setUsername(user.getUsername());
        responseDTO.setRole("USER");
        responseDTO.setToken(token);
        responseDTO.setPhone(user.getPhone());
        responseDTO.setEmail(user.getEmail());
        responseDTO.setAddress(user.getAddress());

        return responseDTO;
    }

    @Override
    public boolean register(RegisterDTO registerDTO) {
        // 验证用户名是否已存在
        if (getByUsername(registerDTO.getUsername()) != null) {
            throw new RuntimeException("用户名已存在");
        }

        // 验证邮箱是否已存在
        if (getByEmail(registerDTO.getEmail()) != null) {
            throw new RuntimeException("邮箱已存在");
        }

        // 验证验证码
        if (!emailService.verifyCode(registerDTO.getEmail(), registerDTO.getCode())) {
            throw new RuntimeException("验证码错误或已过期");
        }

        // 创建用户
        User user = new User();
        user.setUsername(registerDTO.getUsername());
        // 加密密码
        user.setPassword(BCrypt.hashpw(registerDTO.getPassword()));
        user.setEmail(registerDTO.getEmail());
        user.setPhone(registerDTO.getPhone());
        user.setAddress(registerDTO.getAddress());
        user.setStatus(1);

        return save(user);
    }

    @Override
    public Page<User> pageUser(Page<User> page, String keyword) {
        if (StringUtils.isBlank(keyword)) {
            return page(page);
        }
        return baseMapper.pageUser(page, keyword);
    }

    @Override
    public User getByUsername(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        return getOne(wrapper);
    }

    @Override
    public User getByEmail(String email) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getEmail, email);
        return getOne(wrapper);
    }
} 